<?php
class BusAction extends BaseAction
{
    public function index()
    {

    }

    /**
     * 首页Ajax获取商场广告
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBusMallsAdAjax()
    {
        $data = $this->getHomeMallsAd();
        $this->assign('malls', $data['malls']);
        $data['malls'] = $this->fetch();
        $this->ajaxReturn($data, null, 1);
    }

    /**
     * 获取对应商场广告信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function getHomeMallsAd()
    {
        //获取分页商场列表 每次3个商场
        //根据商场获取一级广告 1个
        //根据商场获取二级广告12个
        //获取分页信息
        $malls = array();
        $nowPage = intval($this->_get('p'));
        $cbd = intval($this->_get('cbd'));
        $nowPage = $nowPage ? $nowPage : 1;
        $cacheName = 'home-mall-ad-' . $cbd . '-' . $nowPage;
        $malls = $this->getCityCache($cacheName);
        $pageSize = 3;
        $MarketModel = D('Market');
        $count = $MarketModel->getCityBusMallCount($cbd);
        $allPage = ceil($count / $pageSize);
        if (empty($malls)) {
            if ($nowPage <= $allPage) {
                $firstRow = $pageSize * ($nowPage - 1);
                $malls = $MarketModel->getCityBusMalls($cbd, $firstRow . ',' . $pageSize);
                $AdModel = D('Ad');
                $BrandNewsModel = D('BrandNews');
                foreach ($malls as $key => $mall) {
                    unset($map);
                    $map['m_ad_allot.mid'] = $mall['id'];
                    $map['m_ad.type'] = 1; //广告类型
                    $map['m_ad_allot.level'] = array(array('eq', 1), array('eq', 3), 'or');
                    $mallAdOne = $AdModel->getAd($map, '', '', '0,1');
                    $malls[$key]['ad'] = $mallAdOne;
                    $mallAdTwo = $BrandNewsModel->getListAd(12, $mall['id']);
                    $malls[$key]['ad_new'] = $mallAdTwo;
                    $malls[$key]['url'] = U('/' . $mall['initial']);
                    $malls[$key]['image_url'] = strtolower(UPLOAD_URL . 'malls/' . $mall['initial'] . '.png');
                }
                $this->setCityCache($cacheName, $malls); //set cache
            }
        }
        $data = array();
        $data['malls'] = $malls;
        $data['ap'] = $allPage;
        return $data;
    }

    /**
     * 根据商圈获取商场列表
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBusMallAjax()
    {
        $malls = $this->getBusMall();
        $this->assign('malls', $malls);
        if ($malls) {
            $this->ajaxReturn($this->fetch('getMallListAjax'), 0, 1);
        } else {
            $this->ajaxReturn('暂时还没有商场！', 0, 0);
        }
    }

    /**
     * 获取商圈商场信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBusMall()
    {
        $malls = array();
        $cbd = intval($this->_get('cbd'));
        $cacheName = 'mall-list-' . $cbd;
        $malls = $this->getCityCache($cacheName);
        //获取商场列表
        $MarketModel = D('Market');
        $malls = $MarketModel->getCityBusMalls($cbd);
        if ($malls) {
            $uid = getLoginUserInfo();
            $MarketAttentionModel = D('MarketAttention');
            //获取商场图片
            foreach ($malls as $key => $mall) {
                $malls[$key]['image_url'] = $MarketModel->getMarketImage($mall['image']);
                $malls[$key]['is_att'] = 0;
            }
            $this->setCityCache($cacheName, $malls); //set cache
        }
		if ($uid && $malls) {
            $MarketAttentionModel->checkUserIsAttrMalls($uid, $malls);
        }
        return $malls;
    }
}

?>